package server;

import model.Questions;
import model.User;
import model.UserMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 多线程技术
 */
public class ServerRunnable implements Runnable {
    Socket s;
    ServerDao sd;
    ObjectInputStream ois;
    ObjectOutputStream oos;
    String string = null;

    public ServerRunnable(Socket s, ServerDao sd, ObjectInputStream ois, ObjectOutputStream oos) {
        this.s = s;
        this.sd = sd;
        this.ois = ois;
        this.oos = oos;
        string = s.toString();
    }

    @Override
    public void run() {
        boolean flag = true;
        try {
            //循环接收客户端信息，判断要实现的功能
            while (flag) {
                //接收请求
                String request = ois.readUTF();
                //根据请求判断要进行的业务
                switch (request) {
                    //登陆前
                    case "登录管理员系统":
                        serverReceive();
                        break;
                    case "登录学员系统":
                        serverStudentLogin();
                        break;

                    //学员系统功能,考试完毕后，查询成绩功能和导出成绩功能在客户端完成，不经过服务器
                    case "修改密码":
                        serverUpdateStudentPassword();
                        break;
                    case "客户端退出":
                        System.out.println(string + ois.readObject());
                        oos.writeUTF("服务端已接收到退出请求");
                        flag = false;
                        break;
                    case "开始考试":
                        serverSendQuestions();//发送考题
                        break;
                    //管理员系统
                    case "增加学员":
                        System.out.println("服务器准备增加学员");
                        addUser();
                        break;
                    case "删除学员":
                        serverDeleteStudent();
                        break;
                    case "修改学员":
                        serverUpdateByManager();//管理员修改学员密码
                        break;
                    case "查找学员":
                        serverSearchByName();//查找指定用户名的学员
                        break;
                    case "增加考题":
                        serverAddQuestios();//增加考题
                        break;
                    case "删除考题":
                        System.out.println("服务器准备删除考题");
                        serverDeleteQuestion();
                        break;
                    case "修改考题":
                        serverUpdataQuestion();
                        break;
                    case "查找考题":
                        serverSearchQuestion();
                        break;
                    case "导入考题":
                        serverInputQuestions();
                        break;
                    default:
                        System.out.println("没有这样的功能");
                        break;
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入考题
     */
    private void serverInputQuestions() throws IOException, ClassNotFoundException {
        Map<String, String> inputQuestions = (Map<String, String>) ois.readObject();
        sd.inputQues(inputQuestions);
        oos.writeUTF("导入成功");
        oos.flush();

    }

    /**
     * 查找考题
     */
    private void serverSearchQuestion() throws IOException, ClassNotFoundException {
        String search = ois.readUTF();
        Map<String, String> results = sd.serverSearchQuestions(search);
        oos.writeObject(results);
        oos.flush();
    }

    /**
     * 修改考题
     */
    private void serverUpdataQuestion() throws IOException, ClassNotFoundException {
        //发送所有考题到客户端
        Map<String, String> stringStringMap = sd.readQuestions();
        oos.writeObject(stringStringMap);
        oos.flush();

        String question = ois.readUTF();//获取要修改的考题
        String newquestion = ois.readUTF();//修改后的问题
        String answer = ois.readUTF();//修改后的答案
        System.out.println(question+"----->"+newquestion+"---->"+answer);
        sd.updateQuestion(question, newquestion, answer);
        oos.writeUTF("修改成功");
        oos.flush();
    }

    /**
     * 删除考题
     */
    public void serverDeleteQuestion() throws IOException, ClassNotFoundException {
        //发送所有考题到客户端
        Map<String, String> stringStringMap = sd.readQuestions();
        oos.writeObject(stringStringMap);
        oos.flush();

        String question = ois.readUTF();
        if (sd.deletQuestion(question)) {
            oos.writeUTF("删除考题成功");
        } else {
            oos.writeUTF("删除考题失败");
        }
        oos.flush();


    }

    /**
     * 增加考题
     */
    private void serverAddQuestios() throws IOException, ClassNotFoundException {
        String question = ois.readUTF();
        String answer = ois.readUTF();
        if (sd.addQuestion(question, answer)) {
            oos.writeUTF("添加考题成功");
        } else {
            oos.writeUTF("添加考题失败");
        }
        oos.flush();
    }

    /**
     * 查找学员
     */
    private void serverSearchByName() throws IOException, ClassNotFoundException {
        //接收查找的名字
        String s = ois.readUTF();
        //返回查找结果
        List<User> users = sd.serverSearchByName(s);
        oos.writeObject(users);
        oos.flush();

    }

    /**
     * 管理员修改学员密码
     */
    private void serverUpdateByManager() throws IOException, ClassNotFoundException {
        //1.将所有学员信息发送给客户端
        List<User> users = sd.readUsers();//读取数据
        oos.writeObject(users);//发送数据
        oos.flush();
        User user = (User) ois.readObject();//接收客户端数据
        if (sd.updateByManager(user)) {
            oos.writeUTF("成功");
        } else {
            oos.writeUTF("删除失败,不存在该用户名");
        }
        oos.flush();
    }

    /**
     * 删除学员
     */
    private void serverDeleteStudent() throws IOException, ClassNotFoundException {
        System.out.println(".......");
        //1.将所有学员信息发送给客户端
        List<User> users = sd.readUsers();//读取数据
        oos.writeObject(users);//发送数据
        oos.flush();
        System.out.println(users);
        //接收要删除的用户名
        String name = ois.readUTF();
        //调用删除方法
        if (sd.deleteByName(name)) {
            oos.writeUTF("删除成功");
        } else {
            oos.writeUTF("删除失败,不存在该用户名");
        }
        oos.flush();

    }

    /**
     * 增加学员
     */
    private void addUser() throws IOException, ClassNotFoundException {
        //接收客户端发来的新增学员对象
        User user = (User) ois.readObject();
        //读取服务器系统中的所有学员信息
        if (sd.serverAddStudent(user)) {
            oos.writeUTF("学员信息" + user + "添加成功");
            System.out.println("学员信息" + user + "添加成功");
        } else {
            oos.writeUTF("学员信息" + user + "添加失败，可能服务器中已存在该用户名");
            System.out.println("学员信息" + user + "添加失败，可能服务器中已存在该用户名");
        }
        oos.flush();
    }

    /**
     * 自定义成员方法，实现向客户端发送考试题目
     */
    private void serverSendQuestions() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) ois.readObject();//接收客户端发来的用户信息
        Map<String, String> questions = sd.readQuestions();//读取考题
        oos.writeObject(questions);
        System.out.println("服务器发送考题成功");

    }

    /**
     * 学员自我修改密码
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void serverUpdateStudentPassword() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) ois.readObject();
        System.out.println("要修改的学员信息是：" + tum);
        //调用方法实现学员账户密码修改
        if (sd.updatePassword(tum.getUser())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        oos.writeObject(tum);
        System.out.println("服务器发送学员修改密码结果成功！");
    }

    /**
     * 自定义成员方法接收客户端学员登录请求并校验
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void serverStudentLogin() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) ois.readObject();
        System.out.println("接收到的消息是：" + tum);
        // 调用方法实现学员员账号和密码信息的校验
        if (sd.serverStudentCheck(tum.getUser())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        oos.writeObject(tum);
        System.out.println("服务器发送学员登录校验结果成功！");

    }

    /**
     * 自定义成员方法接收客户端管理员登录请求并检验结果
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) ois.readObject();
        System.out.println("接收到的消息是：" + tum);
        // 调用方法实现管理员账号和密码信息的校验
        if (sd.serverManagerCheck(tum.getUser())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        oos.writeObject(tum);
        System.out.println("服务器发送管理员校验结果成功！");
    }
}
